import { getMouseInfo, EventCode } from './utiles'
import rectanglemark from './rectanglemark'
import drawtools from './drawtools'
import edittools from './edittools'
import polygonmark from './polygonmark'

import _ from 'lodash'


class plot {
  constructor(viewer, options = {}) {
    this._viewer = viewer
    this._options = options
    this._layer = new Cesium.CustomDataSource('plotlayer')
    this._viewer.dataSources.add(this._layer)
    // this._handler = new Cesium.ScreenSpaceEventHandler(
    //   viewer.canvas,
    // )
    this._handler = new Cesium.ScreenSpaceEventHandler(
      viewer.scene.canvas,
    )
    this.drawtools = new drawtools()
    this.edittools = new edittools()
    this.plotlist = {}

    let that = this

    // 默认左键事件
    // 判断点击的是否为标会图层 或者控制点 或者其他
    this._handler.setInputAction((movement) => {
      const picked = that._viewer.scene.pick(movement.position, 3, 3)
      console.log(picked);
      console.log(picked);
      let editpoint = null
      if (picked && picked.id) {
        if (picked.id.id.indexOf('plotmark') > -1) {
          //获取标绘对象开始编辑
          this.edit(this.plotlist[picked.id.id])

          window.isedit = true
          setTimeout(() => {
            window.isedit = false
          }, 200);
          window.editobj = this.plotlist[picked.id.id]

        } else if (picked.id.id.indexOf('editpoint') > -1) {

        } else {
          //结束编辑
          this.edittools.editend()
        }
      } else {
        this.edittools.editend()

      }
    }, EventCode['leftClick'])

    let body = document.querySelector('body') // 这里是把整个页面的鼠标指针都变了，可以指定某个元素范围内

    // 默认hover事件
    this._handler.setInputAction((movement) => {
      const picked = that._viewer.scene.pick(movement.endPosition, 3, 3)
      if (picked && picked.id) {
        if (picked.id.id.indexOf('editpoint') > -1) {
          //获取标绘对象开始编辑
          if (body.style.cursor !== 'crosshair') {
            body.style.cursor = `pointer`
          }
        } else {
          if (body.style.cursor !== 'crosshair') {
            body.style.cursor = `default`
          }
        }
      } else {
        if (body.style.cursor !== 'crosshair') {
          body.style.cursor = `default`
        }
      }
    }, EventCode['mouseMove'])

  }

  draw(o) {
    o._layer = this._layer
    o.plotlist = this.plotlist
    this.drawtools.draw(o)
  }

  edit(e) {
    this.edittools.edit(e)

  }

  deleteitem(id) {
    //
    this._layer.entities.removeById(id)
    delete this.plotlist[id]
  }

  removeall() {
    this._layer.entities.removeAll()
    this.plotlist = {}
  }
  removeallfinal() {
    this._layer.entities.removeAll()
    this.plotlist = {}
    this._handler.destroy()
  }

  loadData(datajson) {

    // 添加

    let entity = null

    datajson.map(f => {


      switch (f.type) {
        case 'polygonmark':
          entity = new polygonmark(window.viewer)
          break;
        case 'rectanglemark':
          entity = new rectanglemark(window.viewer)
          break;

        default:
          break;
      }
      entity.setwgs84pos(f.data)
      this._layer.entities.add(entity.entity)
      this.plotlist[entity.id] = entity
      f.id = entity.id
      f.ide = entity
    })

  }

  toData() {

    let res = []

    _.forIn(this.plotlist, item => {
      res.push({
        name: item.name,
        type: item.type,
        id: item.id,
        data: item.setwgs84pos,
      })

    })

    return res
  }
}

export default plot
